import Vue from 'vue'
import App from './App'
import { router } from './router/index';
import { appRouter } from './router/router';
import store from './store'
import iView from 'iview'
import Util from './libs/util';
import Cookies from 'js-cookie';
import '@/libs/constant'

// import BaiduMap from 'vue-baidu-map'
// Vue.use(BaiduMap, {
//   // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
//   ak: '9jGHjmxMnsh7xACwRsVV1xoufP8SdLcw'
// })
//全局组件
import IButton from '@/components/ui/i-button'
Vue.component('IButton', IButton);

import 'iview/dist/styles/iview.css';
import '@/styles/iview-ui.less';

Vue.use(iView)

Vue.config.productionTip = false

import { sync } from 'vuex-router-sync'
sync(store, router)

new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App },
  mounted () {
    //从localStorage获取缓存好的页面，并设置给cachePage
    this.$store.commit('initCachepage');
    // 检查更新
    // Util.checkUpdate(this);
  },
  created () {
    let tagsList = [];
    //初始化tags,根据appRouter路由来设置，只有设置后，Tab标签才会正常打开
    //初始值已经包含其他路由
    appRouter.map((item) => {
        if (item.children.length <= 1) {
            tagsList.push(item.children[0]);
        } else {
            tagsList.push(...item.children);
        }
    });
    this.$store.commit('setTagsList', tagsList);
    // 显示打开的页面的列表
    this.$store.commit('setOpenedList');
    // console.log(localStorage.pageOpenedList)

    //重新加载权限信息，每次后端权限变化时，刷新浏览器则重新刷新本地权限跟服务器一致
    //之所以放到这里加载，是为了首次加载好权限，这样后面的组件才能正常的进行过滤操作
    this.$store.dispatch("DoGetUserInfo").then(response => {
      if (!response) {
        this.$router.push({ path: "/login" });
      }
    });
  }
})
